package richbar.com.richbar.activity.me;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.base.BaseActivity;
import com.common.util.StringUtil;

import com.okhttp.GlideUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;

import butterknife.BindView;
import butterknife.ButterKnife;
import richbar.com.richbar.R;
import richbar.com.richbar.base.BaseApplication;
import richbar.com.richbar.bean.UserInfoResult;
import richbar.com.richbar.help.TitleHelp;
import richbar.com.richbar.util.LoadingDialog;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.util.Util;
import richbar.com.richbar.view.dialog.ShareDialog;
import richbar.com.richbar.wxapi.WxUtil;

/**
 * Created by Administrator on 2018/11/30.
 * 我的二维码
 */

public class MyQRCodeAc extends BaseActivity {
    @BindView(R.id.head)
    ImageView head;
    @BindView(R.id.iv_qrcode)
    ImageView iv_qrcode;
    @BindView(R.id.tv_name)
    TextView tv_name;
    private LoadingDialog loadingDialog;
    private String path;

    @Override
    public int initLayout() {
        return R.layout.ac_my_qrcode;
    }

    @Override
    public void initTitle() {
        new TitleHelp(this).setTitle("我的二维码").setRightIv(R.mipmap.share, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //WxUtil.getInstance().shareUrl(MyQRCodeAc.this,"www.baidu.com","title","des");

                ShareDialog shareDialog = new ShareDialog(MyQRCodeAc.this, new ShareDialog.OnClickLister() {
                    @Override
                    public void wx1() {
                        WxUtil.getInstance().shareImg(path, 1);
                    }

                    @Override
                    public void wx2() {
                        WxUtil.getInstance().shareImg(path, 2);
                    }
                });
                shareDialog.show();

            }
        });
    }

    @Override
    public void initView() {
        loadingDialog = new LoadingDialog(this, 0);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        getUserInfo();
    }

    private void getUserInfo() {
        loadingDialog.show();
        NetWorkUtil.getInfo(BaseApplication.uid, Util.md5(), new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                final UserInfoResult result = MyGson.getInstance().fromJson(data, UserInfoResult.class);
                if (checkIsOk(result)) {
                    setUserInfo(result);
                    loadingDialog.dismiss();
                }
            }
        });
    }

    private void setUserInfo(UserInfoResult result) {
        final UserInfoResult.DataEntity.InfoEntity bean = result.getData().getInfo();
        GlideUtil.loadImage(this, bean.getHead(), head);
        tv_name.setText(StringUtil.isEmpty(bean.getNickname()) ? "" : bean.getNickname());
        path = NetWorkUtil.URL + bean.getQrcode();
        GlideUtil.loadImage(this, path, iv_qrcode);
        /*new Thread(new Runnable() {
            @Override
            public void run() {
                path = GlideUtil.getInstance().getImgPathFromCache(MyQRCodeAc.this,NetWorkUtil.URL + bean.getQrcode());
            }
        }).start();*/


    }
}
